<?php

/**
 * This is the model class for table "order".
 *
 * The followings are the available columns in table 'order':
 * @property integer $id
 * @property string $createtime
 * @property string $paytime
 * @property string $finishtime
 * @property integer $status
 * @property integer $customerid
 * @property float $amount
 * @property string $currency
 */
class Order extends CommonActiveRecord
{
	const UN_PAY = 0;
	const FINISHED = 1;
	const CANCEL = -1;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Order the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'order';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, status, customerid', 'numerical', 'integerOnly'=>true),
			array('createtime, paytime, finishtime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, createtime, paytime, finishtime, status, customerid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'customer'=>array(self::BELONGS_TO, 'User', 'customerid')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '订单编号',
			'createtime' => '创建时间',
			'paytime' => '支付时间',
			'finishtime' => '完成时间',
			'status' => '状态',
			'orderStatus'=>'订单状态',
			'customerid' => 'Customerid',
			'amount'=>'价格',
			'currency'=>'单位',
			'pay_type'=>'支付方式'
		);
	}

	public function getOrderStatus(){
		if($this->status == 3){
			return '支付成功';
		}else if($this->status == 0){
			return '尚未支付';
		}else if($this->status == -1){
			return '订单取消';
		}else{
			return '支付失败';
		}
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->with = array('customer');
		$criteria->compare('id',$this->id);
		$criteria->compare('createtime',$this->createtime,true);
		$criteria->compare('paytime',$this->paytime,true);
		$criteria->compare('finishtime',$this->finishtime,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('customerid',$this->customerid);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function getUserOrder($customerId){
		$criteria=new CDbCriteria;
		$criteria->order = "createtime desc";
		$criteria->compare('customerid',$customerId);
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
	

	
	public function getOrderProductProvider(){
		$criteria=new CDbCriteria;
		$criteria->with = array('product','product.cover');
		$criteria->compare('orderid', $this->id);
		$dataProvider = new CActiveDataProvider('OrderProductMap',array(
				'criteria'=>$criteria
		));
		return $dataProvider;
	}
	
	/**
	 * 
	 * @param CActiveDataProvider $dataProvider
	 */
	public function getFooter($dataProvider,$field){
		$total = 0;
		foreach($dataProvider->getData() as $data){
			$total+=$data[$field];
		}
		return $total;
	}
	
}